<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectid">
        <el-select v-model="queryParams.projectid" placeholder="项目名称" clearable>
                <el-option
                    v-for="p in projectlist"
                    :key="p.index"
                    :label="p.projectname"
                    :value="p.projectid"
                    @change="handleQuery"
                  />
          </el-select>
      </el-form-item>
      <el-form-item label="答卷名称" prop="examname">
         <el-select v-model="queryParams.examname" placeholder="答卷名称" clearable>
         <el-option
            v-for="term in ResearchExamList"
            :key="term.reseachid"
            :label="term.examname"
            :value="term.examname"
            @change="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出卷人" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入出卷人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入时间" prop="oprtime">
        <el-date-picker clearable
          v-model="queryParams.oprtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择录入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:ResearchExam:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house_sale:ResearchExam:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:ResearchExam:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="importExcelHandle"
        >导入</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:ResearchExam:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ResearchExamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="答卷编号" align="center" prop="reseachid"/>
      <el-table-column label="答卷名称" align="center" prop="examname" >
        <template slot-scope="scope">
          <span @click="handleopen(scope.row)" style="cursor:pointer">{{ scope.row.examname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectname" />
      <el-table-column label="是否启用" align="center" prop="invalid"/>
      <el-table-column label="题目数量" align="center" prop="examcount"/>
      <el-table-column label="出卷人" align="center" prop="userid"/>
      <el-table-column label="录入时间" align="center" prop="oprtime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oprtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:ResearchExam:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:ResearchExam:remove']"
          >删除</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['system:user:resetPwd', 'system:user:edit']">
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>更多
                </span>
                <el-dropdown-menu slot="dropdown">
                   <el-dropdown-item command="handleinfo" icon="el-icon-circle-check"
                    v-hasPermi="['house_sale:ExamMaster:list']">答卷明细</el-dropdown-item>
                </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调研答卷名称对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="projectid">
          <el-select v-model="form.projectid" >
                <el-option
                    v-for="item in projectlist"
                    :key="item.projectid"
                    :label="item.projectname"
                    :value="item.projectid"
                    @change="handleQuery"
                  />
          </el-select>
        </el-form-item>
        <el-form-item label="答卷名称" prop="examname">
          <el-input v-model="form.examname" placeholder="请输入答卷名称" />
        </el-form-item>
        <el-form-item label="是否启用" prop="invalid">
          <el-select v-model="form.invalid" placeholder="是否启用" >
                <el-option
                    v-for="i in invalids"
                    :key="i.index"
                    :label="i"
                    :value="i"
                    @change="handleQuery"
                  />
          </el-select>
        </el-form-item>
        <el-form-item label="序号" prop="orderid">
          <el-input v-model="form.orderid" placeholder="请输入序号" type="number"  min="1"/>
        </el-form-item>
        <el-form-item label="录入时间" prop="oprtime">
          <el-date-picker clearable
            v-model="form.oprtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input type="textarea" v-model="form.description"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--打开试卷-->
    <el-dialog :title="'调研试卷'" :visible.sync="ExamResultVisible" width="700px" :close-on-click-modal="false">
   
    <el-form ref="dev" :model="dev" label-width="80px">
      <h1 style="font-size:30px" align="center">{{dev.examname}}</h1>
      <el-button @click="cancel" style="margin-right:20px;margin-left:40px">关闭</el-button>
      <span style="margin-right:40px">出卷人：<label>{{dev.userid }}&nbsp;&nbsp;&nbsp;&nbsp;</label></span>
      <span style="margin-right:40px">共：<label>{{dev.examcount }}道题&nbsp;&nbsp;&nbsp;&nbsp;</label></span>
      <span>出题时间:<label>{{dev.oprtime}}</label></span>
      <hr color="red" width="100%">
    </el-form> 
    <el-form ref="form" :model="form1"  label-width="80px">
      <el-row>
        <el-col :span="24">
          <div class="dialog-header bg-purple">
            <div v-for="(test, index) in examMastertLists" :key="index" >
              
              <!--题目-->
              <p>{{index + 1}}.{{test.unitname}}</p>

              <!--多选--> <!-- label绑定答案的值,可以绑定索引index,也可以绑定答案内容city -->
              <el-checkbox-group v-if="test.unittype == '多选题'" v-model="form1.result">
                <el-checkbox
                  v-for="(item,index) in test.unitvalue.split(',')"
                  :label="item"
                  :key="index"
                 
                >{{item}}</el-checkbox>
              </el-checkbox-group>
              
              <!--单选-->
              <el-radio-group v-else-if="test.unittype == '单选题'" v-model="form1.result1">
                <el-radio
                 v-for="(item,index) in test.unitvalue.split(',')"
                  :label="index"
                  :key="index"
                  
                >{{item}}</el-radio>
              </el-radio-group> 
            
               <!--简答-->
              <div v-else>
                <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form1.result2"></el-input>
              </div>
            </div>
          </div>
          <br />
        </el-col>
      </el-row>
     </el-form>
      <!-- </el-form> -->
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
     
    </el-dialog>
    <ImportExcel v-if="importVisible" ref="importExcel" @refreshDataList="initData" />
  
  </div>
</template>

<script>
import { listResearchExam, getResearchExam, delResearchExam, addResearchExam, updateResearchExam,projectinfo,listExamMaster} from "@/api/house_sale/ResearchExam";
// import ImportExcel from './importExcel'

export default {
  name: "ResearchExam",
  // components: {
  //   ImportExcel
  // },

  data() {
    return {
      dev: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调研答卷名称表格数据
      ResearchExamList: [],
      // importVisible: false,
      //项目列表
      projectlist: [],
      //试卷题目列表
      examMastertLists: [],
    
      invalids:['启用','停用'],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      ExamResultVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectid: null,
        examname: null,
        invalid: null,
        orderid: null,
        examcount: null,
        description: null,
        userid: null,
        oprtime: null
      },
      // 表单参数
      form: {},
      form1:{
        result:[],
        result1: "",
        result2:""
      },
      // 表单校验
      rules: {
        examname: [
          { required: true, message: "答卷名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created(){
    this.getList();
  },
  methods: {
    /** 查询调研答卷名称列表 */
    getList() {
      this.loading = true;
      listResearchExam(this.queryParams).then(response => {
        this.ResearchExamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      projectinfo().then(response => {
        this.projectlist = response.projectList;
      });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleinfo":
          this.handleinfo(row);
          break;
        default:
          break;
      }
    },
    /** 答卷明细操作 */
    handleinfo: function(row) {
      const reseachid = row.reseachid;
      this.$router.push("/house_sale/ExamMaster/examinfo/" + reseachid);
      console.log(reseachid)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.ExamResultVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reseachid: null,
        projectid: 1,
        examname: null,
        invalid: '启用',
        orderid: null,
        examcount: null,
        description: null,
        userid: this.$store.state.user.name,
        oprtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
     projectinfo().then(response => {
        this.projectlist = response.projectList;
        this.open = true;
        this.title = "添加调研答卷名称";
      });
    },
    /*打开试卷按钮 */
    handleopen(row) {
      const reseachid = row.reseachid
     
      listExamMaster(reseachid).then(response => {
        this.examMastertLists = response.examMastertList;
        console.log(this.examMastertLists)
        console.log(response.examMastertList)
        this.dev = response.researchExam;
        this.ExamResultVisible = true;
      
      });
     
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reseachid = row.reseachid || this.ids
      getResearchExam(reseachid).then(response => {
        this.form = response.researchExam;
        this.projectlist = response.projectList;
        this.open = true;
        this.title = "修改调研答卷名称";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reseachid != null) {
            updateResearchExam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResearchExam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 交卷按钮 */
    submitexam() {
        this.$refs["exam"].validate(valid => {
        if (valid) {
          if (this.exam.resultid != null) {
            updateExamResult(this.exam).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.shijuan = false;
              this.getList();
            });
          } else {
            addExamResult(this.exam).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.shijuan = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reseachids = row.reseachid || this.ids;
      this.$modal.confirm('是否确认删除调研答卷名称编号为"' + reseachids + '"的数据项？').then(function() {
        return delResearchExam(reseachids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/ResearchExam/export', {
        ...this.queryParams
      }, `ResearchExam_${new Date().getTime()}.xlsx`)
    },
    // 导入
    importExcelHandle() {
      this.importVisible = true
      this.$nextTick(() => {
        this.$refs.importExcel.init()
      })
    },
    // // 用户状态修改
    // handleStatusChange(row) {
    //   let text = row.invalid === "0" ? "启用" : "停用";
    //   this.$modal.confirm('确认要' + text + ''+'试卷编号为' + row.reseachid + '调研答卷吗？').then(function() {
    //     return changeinvalid(row.reseachid, row.invalid);
    //   }).then(() => {
    //     this.$modal.msgSuccess(text + "成功");
    //   }).catch(function() {
    //     row.invalid = row.invalid === "0" ? "1" : "0";
    //   });
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reseachid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    }
  }
};
</script>
